<?php
/**
 * @copyright Copyright (c) 2019
 */

namespace lujie\proxy;


class HttpHelper
{
    /**
     * @param string $buffer
     * @return array
     */
    public static function parseHeader(string $buffer): ?array
    {
        $headerEndPos = strpos($buffer, "\r\n\r\n");
        if ($headerEndPos === false) {
            return null;
        }
        $headers = [];
        $headerLines = array_filter(explode("\r\n", $buffer));
        $firstLine = array_shift($headerLines);
        [$headers['method'], $headers['address'], $headers['httpVersion']] = explode(' ', $firstLine);
        foreach ($headerLines as $headerLine) {
            [$key, $value] = explode(': ', $headerLine);
            $headers[$key] = $value;
        }
        return $headers;
    }
}